Welcome to a quick exercise for you to practice your pandas skills! We will be using the SF Salaries Dataset from Kaggle! Just follow along and complete the tasks outlined in bold below. The tasks will get harder and harder as you go along.
Import pandas as pd.
In [18]:
import pandas as pd
Read Salaries.csv as a dataframe called sal.
In [19]:
sal = pd.read_csv('Salaries.csv')
Check the head of the DataFrame.
In [20]:
sal.head()
Out[20]:
Use the .info() method to find out how many entries there are.
In [73]:
sal.info() # 148654 Entries
What is the average BasePay ?
In [74]:
sal['BasePay'].mean()
Out[74]:
What is the highest amount of OvertimePay in the dataset ?
In [75]:
sal['OvertimePay'].max()
Out[75]:
What is the job title of Joseph Driscoll ?
In [58]:
sal[sal['EmployeeName']=='JOSEPH DRISCOLL']['JobTitle']
Out[58]:
How much does Joseph Dricoll make (including benefits)?
In [70]:
sal[sal['EmployeeName']=='JOSEPH DRISCOLL']['TotalPayBenefits']
Out[70]:
What is the name of highest paid person (including benefits)?
In [60]:
sal[sal['TotalPayBenefits']== sal['TotalPayBenefits'].max()] #['EmployeeName']
# or
# sal.loc[sal['TotalPayBenefits'].idxmax()]
Out[60]:
What is the name of lowest paid person (including benefits)?
In [24]:
sal[sal['TotalPayBenefits']== sal['TotalPayBenefits'].min()] #['EmployeeName']
# or
# sal.loc[sal['TotalPayBenefits'].idxmax()]['EmployeeName']
Out[24]:
What was the average (mean) BasePay of all employees per year? (2011-2014) ?
In [17]:
sal.groupby('Year').mean()['BasePay']
Out[17]:
How many unique job titles are there?
In [27]:
sal['JobTitle'].nunique()
Out[27]:
What are the top 5 most common jobs?
In [37]:
sal['JobTitle'].value_counts().head(5)
Out[37]:
How many Job Titles were represented by only one person in 2013? (e.g. Job Titles with only one occurence in 2013?)
In [51]:
sum(sal[sal['Year']==2013]['JobTitle'].value_counts() == 1) # pretty tricky way to do this...
Out[51]:
How many people have the word Chief in their job title? (This is pretty tricky)
In [67]:
def chief_string(title):
if 'chief' in title.lower().split():
return True
else:
return False
In [69]:
sum(sal['JobTitle'].apply(lambda x: chief_string(x)))
Out[69]:
Bonus: Is there a correlation between length of the Job Title string and Salary?
In [77]:
sal['title_len'] = sal['JobTitle'].apply(len)
In [81]:
sal[['title_len','TotalPayBenefits']].corr() # No correlation.
Out[81]: